package com.cxb.demotest.mq.rocketmq.filter;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;


/**
 * sql过滤消息生产者
 * @author Xb
 */
public class SqlFilterProducer {
    public static void main(String[] args) throws Exception {
        // 实例化消息生产者
        DefaultMQProducer producer = new DefaultMQProducer("SqlFilterProducer");

        // 设置nameserver的地址
        producer.setNamesrvAddr("127.0.0.1:9876");

        // 启动producer实例
        producer.start();

        // 设定三种标签
        String[] tags = new String[]{"TagA", "TagB", "TagC"};

        for (int i = 0; i < 10; i++) {
            // 创建消息，执行topic，tag，消息体
            Message message = new Message(
                    "SqlFilterTopic",
                    tags[i % tags.length],
                    ("Hello, filter message!"+i).getBytes(RemotingHelper.DEFAULT_CHARSET)
            );

            // 设置sql过滤的属性
            message.putUserProperty("a", String.valueOf(i));
            SendResult sendResult = producer.send(message);
            System.out.printf("%s%n", sendResult);
        }

        // 关闭生产者实例
        producer.shutdown();

    }

}
